#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int minSubArrayLen(int s, vector<int> &nums) {
        auto n = nums.size();
        if (n == 0) {
            return 0;
        }
        // 这个地方要申请n+1， 因为第一处位置的前缀和是0
        vector<int> sums(n + 1, 0);

        for (int i = 1; i <= n; ++i) {
            sums[i] = nums[i - 1] + sums[i - 1];
        }

        int ans = INT_MAX;
        for (int i = 1; i <= n; ++i) {
            int target = s + sums[i - 1];
            auto bound = lower_bound(sums.begin(), sums.end(), target);
            if (bound != sums.end()) {
                ans = min(ans, static_cast<int>(bound - sums.begin()) - (i - 1));
            }
        }
        return ans == INT_MAX ? 0 : ans;
    }
};

int main() {

    vector<int> nums{2, 3, 1, 2, 4, 3};
    int target = 7;
    Solution solution;
    auto result = solution.minSubArrayLen(target, nums);
    cout << result << endl;

    return 0;
}